home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-11-19 | 6.5 KB | 237 lines | [TEXT/MPS ] |
- {
- File: SampleLibrary.inc.p
-
- Contains: Implementation of the PSampleLibrary shared library.
-
- Copyright: © 1993-1994 by Apple Computer, Inc., all rights reserved.
-
- }
-
- VAR
- gLightState : BOOLEAN; { state of the traffic light }
- gWindow : WINDOWPTR; { global pointer to our traffic window }
- gStopRect : Rect; { rectangle for stop light, set up by NewTrafficLight }
- gGoRect : Rect; { rectangle for go light, set up by NewTrafficLight }
-
- (*————————————————————————————————————————————————————————————————————————————————————
- GoGetRect
-
- loads the global rectrangles that are used for drawing the traffic lights.
- ————————————————————————————————————————————————————————————————————————————————————*)
-
- FUNCTION GoGetRect(rectID: INTEGER; VAR theRect: Rect) : BOOLEAN;
-
- TYPE
- RectPtr = ^Rect;
- RectHnd = ^RectPtr;
- VAR
- resource : Handle;
-
- BEGIN
- resource := GetResource('RECT', rectID);
- IF resource <> NIL THEN BEGIN
- GoGetRect := TRUE;
- theRect := RectHnd(resource)^^;
- END
- ELSE
- GoGetRect := FALSE;
- END; {GoGetRect}
-
- (*————————————————————————————————————————————————————————————————————————————————————
- NewTrafficLight
-
- allocate the traffic window and initilize the traffic light globals.
- ————————————————————————————————————————————————————————————————————————————————————*)
-
- FUNCTION NewTrafficLight : OSErr;
- VAR
- savedrefnum : LONGINT;
- err : OSErr;
- themenu : MenuHandle;
- libraryfile : TLibraryFilePtr;
- gotrect : BOOLEAN;
-
- BEGIN
- gLightState := false;
-
- { include the library's file in resource chain so we can allocate our menu
- and window resources }
-
- libraryfile := GetLocalLibraryFile;
-
- err := Preflight( libraryfile, savedrefnum );
-
- IF ( err = kNoError ) THEN BEGIN
-
- gWindow := GetNewWindow( rWindow, NIL, WindowPtr(-1) );
-
- gotrect := GoGetRect(rStopRect, gStopRect);{ get rectangle for Stop light }
- gotrect := GoGetRect(rGoRect, gGoRect); { get rectangle for Go light }
-
- { if there is a MENU resources in our shared library we should append it
- to our application }
-
- themenu := GetMenu( mLight ); { get our traffic light menu }
- InsertMenu( themenu, 0 ); { append to the application's menu }
-
- DrawMenuBar; { go ahead and update the menu bar }
-
- { Now we are done with accessing our library resource; restore the chain
- to it previous state }
-
- err := Postflight( libraryfile, savedrefnum );
-
- END;
-
- NewTrafficLight := err;
-
- END;
-
- (*————————————————————————————————————————————————————————————————————————————————————
- FreeTrafficLight
-
- we are done, so dispose the windowrecord
- ————————————————————————————————————————————————————————————————————————————————————*)
-
- PROCEDURE FreeTrafficLight;
- BEGIN
- DisposeWindow( gWindow ); { we are done}
- END;
-
- (*————————————————————————————————————————————————————————————————————————————————————
- GetLightState
-
- return current state of traffic light
- ————————————————————————————————————————————————————————————————————————————————————*)
-
- FUNCTION GetLightState : BOOLEAN;
- BEGIN
- GetLightState := gLightState; { return the state of Traffic Light }
- END;
-
- (*————————————————————————————————————————————————————————————————————————————————————
- SetLightState
-
- set the light to the new state
- ————————————————————————————————————————————————————————————————————————————————————*)
-
- PROCEDURE SetLightState( newState : BOOLEAN );
- BEGIN
- gLightState := newState; { set the new state }
-
- SetPort(gWindow); { make sure we are at the right port before }
- InvalRect( gWindow^.portRect); { invalidate update region }
- END;
-
- (*————————————————————————————————————————————————————————————————————————————————————
- DrawLight
-
- draw the actual traffic light
- ————————————————————————————————————————————————————————————————————————————————————*)
-
- PROCEDURE DrawLight;
- VAR
- oldport : GrafPtr;
- BEGIN
-
- IF( gWindow <> NIL ) THEN BEGIN
-
- GetPort( oldport ); { save the old port }
- SetPort( gWindow ); { set the new port to our object's port }
-
- EraseRect(gWindow^.portRect); { clear out any garbage that may linger }
- IF ( gLightState ) THEN { draw a red (or white) stop light }
- ForeColor(redColor)
- ELSE
- ForeColor(whiteColor);
-
- PenSize( 2, 2 );
- PaintOval(gStopRect);
- ForeColor(blackColor);
- FrameOval(gStopRect);
- IF gLightState = FALSE THEN { draw a green (or white) go light }
- ForeColor(greenColor)
- ELSE
- ForeColor(whiteColor);
- PaintOval(gGoRect);
- ForeColor(blackColor);
- FrameOval(gGoRect);
- PenSize( 1, 1 );
-
- SetPort( oldport ); { restore the grafpointer }
- END;
- END;
-
- (*————————————————————————————————————————————————————————————————————————————————————
- AdjustTrafficLightMenus
-
- Enable or disable traffic light's menus based on the current state.
- ————————————————————————————————————————————————————————————————————————————————————*)
-
- PROCEDURE AdjustTrafficLightMenus( active: BOOLEAN );
-
- VAR
- menu : MenuHandle;
- savedrefnum : LONGINT;
- libraryfile : TLibraryFilePtr;
- err : OSErr;
-
- BEGIN
- { include the library in resource chain so we can allocate our menu resources }
-
- libraryfile := GetLocalLibraryFile;
- err := Preflight( libraryfile, savedrefnum );
-
- IF( err = noErr ) THEN BEGIN
- menu := GetMenuHandle( mLight );
- IF( menu <> NIL ) THEN BEGIN { get the handle to traffic light menu }
-
- IF ( active ) THEN BEGIN { do we need to enable them? }
- EnableItem(menu, iStop);
- EnableItem(menu, iGo);
- END
- ELSE BEGIN
- DisableItem(menu, iStop);
- DisableItem(menu, iGo);
- END;
-
- CheckItem(menu, iStop, gLightState); { we can also determine check/uncheck state, too }
- CheckItem(menu, iGo, NOT gLightState);
- END;
-
- { restore the resource chain back to it previous state before preflight }
- err := Postflight( libraryfile, savedrefnum );
- END;
-
- END;
-
- (*————————————————————————————————————————————————————————————————————————————————————
- DoTrafficLightMenuCommand
-
- Enable and disable menus based on the current state of traffic light.
- ————————————————————————————————————————————————————————————————————————————————————*)
-
- PROCEDURE DoTrafficLightMenuCommand( menuItem: INTEGER );
-
- VAR
- savedrefnum : LONGINT;
- libraryfile : TLibraryFilePtr;
- err : OSErr;
-
- BEGIN
-
- { include the library in resource chain so we can allocate our menu resources }
-
- libraryfile := GetLocalLibraryFile;
- err := Preflight( libraryfile, savedrefnum );
-
- CASE menuItem OF
- iStop:
- SetLightState( TRUE ); { set new state and update }
- iGo:
- SetLightState( FALSE ); { set new staet and update }
- END;
- { restore the resource chain back to it previous state before preflight }
- err := Postflight( libraryfile, savedrefnum );
- END;
-